package com.sc.search;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class InitIndex {
    @Autowired
    private RestHighLevelClient client;

    String json = "{\n" +
            "    \"mappings\":{\n" +
            "        \"properties\":{\n" +
            "            \"id\":{\n" +
            "                \"type\":\"long\"\n" +
            "            },\n" +
            "            \"publishTime\":{\n" +
            "                \"type\":\"date\"\n" +
            "            },\n" +
            "            \"layout\":{\n" +
            "                \"type\":\"integer\"\n" +
            "            },\n" +
            "            \"images\":{\n" +
            "                \"type\":\"keyword\",\n" +
            "                \"index\": false\n" +
            "            },\n" +
            "            \"staticUrl\":{\n" +
            "                \"type\":\"keyword\",\n" +
            "                \"index\": false\n" +
            "            },\n" +
            "            \"authorId\": {\n" +
            "                \"type\": \"long\"\n" +
            "            },\n" +
            "            \"authorName\": {\n" +
            "                \"type\": \"text\"\n" +
            "            },\n" +
            "            \"title\":{\n" +
            "                \"type\":\"text\",\n" +
            "                \"analyzer\":\"ik_smart\"\n" +
            "            },\n" +
            "            \"content\":{\n" +
            "                \"type\":\"text\",\n" +
            "                \"analyzer\":\"ik_smart\"\n" +
            "            }\n" +
            "        }\n" +
            "    }\n" +
            "}";

    @Test
    public void initIndex() throws Exception {
        // 创建索引
        CreateIndexRequest request = new CreateIndexRequest("article_info");
        request.source(json, XContentType.JSON);
        client.indices().create(request, RequestOptions.DEFAULT);
    }
}
